<template>
  <div class="process-config-tabs">
    <el-tabs v-model="activeName" type="border-card">
      <el-tab-pane label="基础设置" name="basic">
        <basic-tab ref="basicTab" />
      </el-tab-pane>
      <el-tab-pane label="下单" name="order">
        <order-tab ref="orderTab" />
      </el-tab-pane>
      <el-tab-pane label="库存" name="store">
        <store-tab ref="storeTab" />
      </el-tab-pane>
      <el-tab-pane label="配送" name="distribution">
        <distribution-tab ref="distributionTab" />
      </el-tab-pane>
      <el-tab-pane label="结算" name="settlement">
        <settlement-tab ref="settlementTab" />
      </el-tab-pane>
      <el-tab-pane label="微信配置" name="wechat">
        <wechat-tab ref="wechatTab" />
      </el-tab-pane>
    </el-tabs>
    <div class="btn">
      <el-button type="primary" :loading="$loadingPlugin.save" @click="save">保存配置</el-button>
    </div>
  </div>
</template>

<script>
  import { querySaveProcessConfig } from '@/api'
  import moment from 'dayjs'
  import BasicTab from './BasicTab'
  import OrderTab from './OrderTab'
  import StoreTab from './StoreTab'
  import DistributionTab from './DistributionTab'
  import SettlementTab from './SettlementTab'
  import WechatTab from './WechatTab'
  import { keepDotLength } from '@/utils/math'
  import store from '@/store'

  export default {
    name: 'processConfig',
    components: {
      BasicTab, OrderTab, StoreTab, DistributionTab, SettlementTab, WechatTab
    },
    data() {
      return {
        activeName: 'basic'
      }
    },
    methods: {
      async save() {
        const params = {
          pbxURL: this.$refs.basicTab.query.pbxURL,
          monthlyDayNum: this.$refs.orderTab.query.monthlyDayNum,
          buyMax: this.$refs.orderTab.query.buyMax || 999,
          exchangeBuyMax: this.$refs.orderTab.query.exchangeBuyMax || 999,
          limitSpare: this.$refs.orderTab.query.limitSpare || 0,
          wxLimitTime: moment(this.$refs.wechatTab.time[0]).format('hh:mm') + '-' + moment(this.$refs.wechatTab.time[1]).format('hh:mm'),
          wxUpMin: this.$refs.wechatTab.query.wxUpMin << 1,
          sp1: 0,
          sp2: 0,
          registerTips: this.$refs.orderTab.query.registerTips
        }
        if (params.limitSpare > 480) {
          this.$message.warning('微信下单未支付限时作废时间不可超过最大值480')
          return
        }
        params.fee1 = (keepDotLength((this.$refs.basicTab.query.fee1 || 0), 2) * 100).toFixed(0) // 上楼基础费
        if (this.$refs.orderTab.query.ck_007_sys_setting_1_1) params.sp1 |= 1 << 1
        if (this.$refs.orderTab.query.ck_007_sys_setting_1_2) params.sp1 |= 1 << 2
        if (this.$refs.orderTab.query.ck_007_sys_setting_1_3) params.sp1 |= 1 << 3
        if (this.$refs.orderTab.query.ck_007_sys_setting_1_4) params.sp1 |= 1 << 4
        if (this.$refs.basicTab.query.ck_007_sys_setting_1_5) params.sp1 |= 1 << 5
        if (this.$refs.orderTab.query.ck_007_sys_setting_1_6) params.sp1 |= 1 << 6
        if (this.$refs.orderTab.query.ck_007_sys_setting_1_7) params.sp1 |= 1 << 7
        if (this.$refs.orderTab.query.ck_007_sys_setting_1_8) params.sp1 |= 1 << 8
        if (this.$refs.orderTab.query.ck_007_sys_setting_1_9) params.sp1 |= 1 << 9
        if (this.$refs.orderTab.query.ck_007_sys_setting_1_10) params.sp1 |= 1 << 10
        if (this.$refs.orderTab.query.ck_007_sys_setting_1_11) params.sp1 |= 1 << 11
        if (this.$refs.orderTab.query.le > 0) params.sp1 |= (((this.$refs.orderTab.query.le & 0x07) - 1) << 12)
        if (this.$refs.basicTab.query.ck_007_sys_setting_1_15) params.sp1 |= 1 << 15
        if (this.$refs.orderTab.query.ck_007_sys_setting_1_16) params.sp1 |= 1 << 16
        if (this.$refs.orderTab.query.ck_007_sys_setting_1_17) params.sp1 |= 1 << 17
        if (this.$refs.basicTab.query.maxFloor > 0) {
          params.sp1 |= ((this.$refs.basicTab.query.maxFloor & 0xF) << 18)
        }
        if (this.$refs.basicTab.query.ck_007_sys_setting_1_22) params.sp1 |= 1 << 22
        if (this.$refs.orderTab.query.ck_007_sys_setting_1_23) params.sp1 |= 1 << 23
        if (this.$refs.orderTab.query.ck_007_sys_setting_1_24) params.sp1 |= 1 << 24
        if (this.$refs.orderTab.query.ck_007_sys_setting_1_26) params.sp1 |= 1 << 26
        if (this.$refs.orderTab.query.ck_007_sys_setting_1_27) params.sp1 |= 1 << 27
        if (this.$refs.basicTab.query.ck_007_sys_setting_1_28) params.sp1 |= 1 << 28
        if (this.$refs.orderTab.query.ck_007_sys_setting_1_29) params.sp1 |= 1 << 29
        if (this.$refs.basicTab.query.ck_007_sys_setting_1_30) params.sp1 |= 1 << 30
        params.wxUpMin |= 1
        if (this.$refs.settlementTab.query.ck_007_sys_setting_2_30) params.sp2 |= 1 << 30
        if (this.$refs.distributionTab.query.ck_007_sys_setting_2_0) params.sp2 |= 1
        if (this.$refs.distributionTab.query.ck_007_sys_setting_2_2) params.sp2 |= 1 << 2
        if (this.$refs.distributionTab.query.ck_007_sys_setting_2_3) params.sp2 |= 1 << 3
        // 天数 int 值
        const days = this.$refs.distributionTab.query.days
        if (days > 0) {
          if (days > 7) {
            params.sp2 |= (((7 & 0x07) - 1) << 4)
          } else {
            params.sp2 |= (((days & 0x07) - 1) << 4)
          }
          params.billMaxDate = days
        }
        if (this.$refs.distributionTab.query.ck_007_sys_setting_2_7) params.sp2 |= 1 << 7
        if (this.$refs.distributionTab.query.ck_007_sys_setting_2_8) params.sp2 |= 1 << 8
        if (this.$refs.distributionTab.query.ck_007_sys_setting_2_9) params.sp2 |= 1 << 9
        if (this.$refs.distributionTab.query.ck_007_sys_setting_2_10) params.sp2 |= 1 << 10
        if (this.$refs.distributionTab.query.ck_007_sys_setting_2_11) params.sp2 |= 1 << 11
        if (this.$refs.storeTab.query.ck_007_sys_setting_2_12) params.sp2 |= 1 << 12
        if (this.$refs.settlementTab.query.ck_007_sys_setting_2_13) params.sp2 |= 1 << 13
        if (this.$refs.distributionTab.query.ck_007_sys_setting_2_14) params.sp2 |= 1 << 14
        if (this.$refs.distributionTab.query.ck_007_sys_setting_2_15) params.sp2 |= 1 << 15
        if (this.$refs.basicTab.query.ck_007_sys_setting_2_16) params.sp2 |= 1 << 16
        if (this.$refs.distributionTab.query.ck_007_sys_setting_2_17) params.sp2 |= 1 << 17
        if (this.$refs.settlementTab.query.ck_007_sys_setting_2_18) params.sp2 |= 1 << 18
        if (this.$refs.settlementTab.query.ck_007_sys_setting_2_19) params.sp2 |= 1 << 19
        if (this.$refs.settlementTab.query.ck_007_sys_setting_2_20) params.sp2 |= 1 << 20
        if (this.$refs.distributionTab.query.ck_007_sys_setting_2_21) params.sp2 |= 1 << 21
        if (this.$refs.basicTab.query.ck_007_sys_setting_2_24) params.sp2 |= 1 << 24
        if (this.$refs.distributionTab.query.ck_007_sys_setting_2_25) params.sp2 |= 1 << 25
        if (this.$refs.basicTab.query.ck_007_sys_setting_2_26) params.sp2 |= 1 << 26
        if (this.$refs.orderTab.query.ck_007_sys_setting_2_27) params.sp2 |= 1 << 27
        if (this.$refs.orderTab.query.ck_007_sys_setting_2_28) params.sp2 |= 1 << 28
        if (this.$refs.distributionTab.query.ck_007_sys_setting_3_0) params.sp3 |= 1
        if (this.$refs.distributionTab.query.ck_007_sys_setting_3_1) params.sp3 |= 1 << 1
        if (this.$refs.orderTab.query.ck_007_sys_setting_3_2) params.sp3 |= 1 << 2
        if (this.$refs.storeTab.query.radio === '1') params.sp3 |= 1 << 3
        if (this.$refs.orderTab.query.ck_007_sys_setting_3_4) params.sp3 |= 1 << 4
        if (this.$refs.orderTab.query.ck_007_sys_setting_3_5) params.sp3 |= 1 << 5
        if (this.$refs.storeTab.query.ck_007_sys_setting_3_6) params.sp3 |= 1 << 6
        if (this.$refs.storeTab.query.ck_007_sys_setting_3_7) params.sp3 |= 1 << 7
        if (this.$refs.basicTab.query.ck_007_sys_setting_3_8) params.sp3 |= 1 << 8
        if (this.$refs.orderTab.query.ck_007_sys_setting_3_9) params.sp3 |= 1 << 9
        if (this.$refs.basicTab.query.ck_007_sys_setting_3_10) params.sp3 |= 1 << 10
        if (this.$refs.basicTab.query.ck_007_sys_setting_3_14) params.sp3 |= 1 << 14
        if (this.$refs.orderTab.query.ck_007_sys_setting_3_15) params.sp3 |= 1 << 15
        if (this.$refs.distributionTab.query.ck_007_sys_setting_3_16) params.sp3 |= 1 << 16
        if (this.$refs.distributionTab.query.ck_007_sys_setting_3_17) params.sp3 |= 1 << 17
        if (this.$refs.basicTab.query.ck_007_sys_setting_3_18) params.sp3 |= 1 << 18
        if (this.$refs.basicTab.query.ck_007_sys_setting_3_19) params.sp3 |= 1 << 19
        if (this.$refs.wechatTab.query.ck_007_sys_setting_3_20) params.sp3 |= 1 << 20
        if (this.$refs.wechatTab.query.ck_007_sys_setting_3_21) params.sp3 |= 1 << 21
        if (this.$refs.wechatTab.query.ck_007_sys_setting_3_22) params.sp3 |= 1 << 22
        if (this.$refs.wechatTab.query.ck_007_sys_setting_3_23) params.sp3 |= 1 << 23
        params.sp3 |= 1 << 25
        if (this.$refs.wechatTab.query.ck_007_sys_setting_3_27) params.sp3 |= 1 << 27
        if (this.$refs.basicTab.query.ck_007_sys_setting_3_31) params.sp3 |= 1 << 31
        if (this.$refs.distributionTab.query.ck_007_sys_setting_sp4_0) params.sp4 |= 1
        if (this.$refs.basicTab.query.ck_007_sys_setting_sp4_1) params.sp4 |= 1 << 1
        if (this.$refs.basicTab.query.ck_007_sys_setting_sp4_2) params.sp4 |= 1 << 2
        if (this.$refs.wechatTab.query.ck_007_sys_setting_sp4_4) params.sp4 |= 1 << 4
        if (this.$refs.orderTab.query.ck_007_sys_setting_sp4_5) params.sp4 |= 1 << 5
        if (this.$refs.distributionTab.query.ck_007_sys_setting_sp4_6) params.sp4 |= 1 << 6
        if (this.$refs.wechatTab.query.ck_007_sys_setting_sp4_7) params.sp4 |= 1 << 7
        if (this.$refs.wechatTab.query.ck_007_sys_setting_sp4_8) params.sp4 |= 1 << 8
        if (this.$refs.orderTab.query.ck_007_sys_setting_sp4_9) params.sp4 |= 1 << 9
        if (this.$refs.wechatTab.query.ck_007_sys_setting_sp4_12) params.sp4 |= 1 << 12
        if (this.$refs.wechatTab.query.ck_007_sys_setting_sp4_13) params.sp4 |= 1 << 13
        if (this.$refs.wechatTab.query.ck_007_sys_setting_sp4_14) params.sp4 |= 1 << 14
        if (this.$refs.orderTab.query.ck_007_sys_setting_sp4_16) params.sp4 |= 1 << 16
        if (this.$refs.orderTab.query.ck_007_sys_setting_sp4_17) params.sp4 |= 1 << 17
        if (this.$refs.orderTab.query.ck_007_sys_setting_sp4_20) params.sp4 |= 1 << 20
        if (this.$refs.settlementTab.query.ck_007_sys_setting_sp4_26) params.sp4 |= 1 << 26
        if (this.$refs.basicTab.query.ck_007_sys_setting_5_0) params.sp5 |= 1 << 0
        if (this.$refs.basicTab.query.ck_007_sys_setting_5_1) params.sp5 |= 1 << 1
        if (this.$refs.basicTab.query.ck_007_sys_setting_5_2) params.sp5 |= 1 << 2
        if (this.$refs.basicTab.query.ck_007_sys_setting_5_3) params.sp5 |= 1 << 3
        if (this.$refs.basicTab.query.ck_007_sys_setting_5_4) params.sp5 |= 1 << 4
        if (this.$refs.basicTab.query.ck_007_sys_setting_5_5) params.sp5 |= 1 << 5
        if (this.$refs.basicTab.query.ck_007_sys_setting_5_6) params.sp5 |= 1 << 6
        if (this.$refs.basicTab.query.ck_007_sys_setting_5_7) params.sp5 |= 1 << 7
        if (this.$refs.basicTab.query.ck_007_sys_setting_5_8) params.sp5 |= 1 << 8
        if (this.$refs.basicTab.query.ck_007_sys_setting_5_9) params.sp5 |= 1 << 9
        if (this.$refs.basicTab.query.ck_007_sys_setting_5_10) params.sp5 |= 1 << 10
        if (this.$refs.basicTab.query.ck_007_sys_setting_5_11) params.sp5 |= 1 << 11
        if (this.$refs.basicTab.query.ck_007_sys_setting_5_12) params.sp5 |= 1 << 12
        if (this.$refs.basicTab.query.ck_007_sys_setting_5_13) params.sp5 |= 1 << 13
        if (this.$refs.basicTab.query.ck_007_sys_setting_5_14) params.sp5 |= 1 << 14
        const hour = this.$refs.settlementTab.query.hour
        let time
        if (hour === 24) {
          time = '2000-1-1 00:00:00'
        } else {
          time = `2000-1-1 ${String(hour).padStart(2, '0')}:00:00`
        }
        if (this.$refs.settlementTab.query.ck_007_sys_setting_time1) params.time1 = moment(time).format()
        if (this.$refs.settlementTab.query.ck_007_sys_setting_time2) params.time2 = moment(time).format()
        const info = this.$refs.wechatTab.query.validateContent
        if (info === 1) {
          // 仅校验手机号
          params.sp3 |= 1 << 28
        }
        if (info === 2) {
          // 校验手机号和客户编号
          params.sp3 |= 1 << 28
          params.sp3 |= 1 << 29
        }
        if (info === 3) {
          // 校验手机号和客户名称
          params.sp3 |= 1 << 28
          params.sp3 |= 1 << 30
        }
        if (info === 4) {
          // 校验手机号，客户名称，客户编号
          params.sp3 |= 1 << 28
          params.sp3 |= 1 << 29
          params.sp3 |= 1 << 30
        }
        const info1 = this.$refs.basicTab.chipSupport
        if (info1 === 2) {
          // 选中华申
          params.sp4 |= 1 << 22
        }
        if (info1 === 3) {
          // 选中祥康
          params.sp4 |= 1 << 23
        }
        await querySaveProcessConfig(params)
        await store.dispatch('user/getCorpSettingData')
        this.$notify({
          title: '提示',
          message: '保存成功',
          type: 'success',
          duration: 2000
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.process-config-tabs {
  height: 100%;
  .el-tabs {
    height: calc(100% - 100px);
    overflow: auto;
  }
  .btn {
    margin-top: 20px;
    text-align: center;
  }
}
</style>
